<?php
/**
* @myIPN for sobi 2
* @Copyright (C) 2007 Ethan Chai Voon Chong, http://www.ethansoft.com
* @All Rights Reserved
*/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

require_once( $mainframe->getPath( 'admin_html' ) );
require_once ( $mainframe->getPath( 'class' ) );

$task = mosGetParam( $_REQUEST, 'task', '' );

switch ($task){
	case "new":
	case "edit":
		editEntry();
		break;
	case "apply":
	case "save":
		saveEntry();
		break;
	case 'remove':
		removeEntry();
		break;
	case "cancel":
		cancelEdit();
		break;
	case "config":
		editConfig();
		break;
	case "applyconfig":
	case "saveconfig":
		saveConfig();
		break;
	case "log":
		HTML_output::showInfo("../components/com_myipn/log.txt");
		break;
	case "deletefile":
		HTML_output::deleteFile("../components/com_myipn/log.txt");
                break;
	case 'about':
		HTML_output::showAbout();
		break;
	default:
		listEntries();
		break;
}

function listEntries() {
    global $database, $mainframe, $option;

	$status = trim( strtolower( mosGetParam( $_POST, 'status', '' ) ) );
	$search = trim( strtolower( mosGetParam( $_POST, 'search', '' ) ) );
	$limit = $mainframe->getUserStateFromRequest( "viewlistlimit", 'limit', 10 );
	$limitstart = $mainframe->getUserStateFromRequest( "view{$option}limitstart", 'limitstart', 0 );

	$query = "SELECT count(id) "
                ."FROM #__myipn_sub_paypal_log ";
	if ($search != "") {
		$query .= " WHERE ( payer_email LIKE '%$search%' OR username LIKE '%$search%')";
	}
	if ( $status != "") {
	  	if ($search != ""){
			if ( $status != "all")
			$query .= " AND payment_status LIKE '%$status%'";
			}
	 	else
		{
			if ( $status != "all")
			$query .= " WHERE payment_status LIKE '%$status%'";
		}
	}
	$database->setQuery($query);
	$total = $database->loadResult();
	echo $database->getErrorMsg();

	if ($limit > $total) {
		$limitstart = 0;
	}

	$query = "SELECT * "
	."FROM #__myipn_sub_paypal_log ";
	
	if ($search != "") {
		$query .= " WHERE ( payer_email LIKE '%$search%' OR username LIKE '%$search%')";
	}
	
	if ( $status != "") {
		
                if ($search != "")
		{
			if ( $status != "all")
                        $query .= " AND payment_status LIKE '%$status%'";
                }
                else
                {
			if ( $status != "all")
                        $query .= " WHERE payment_status LIKE '%$status%'";
                }
        }
	
	
	//echo  $query;exit();
	$query .= " ORDER BY id ";
	$query .= " LIMIT $limitstart, $limit";

	$database->setQuery( $query );
	$rows = $database->loadObjectList();
	if ($database->getErrorNum()) {
		echo $database->stderr();
		return false;
	}

	//echo "<pre>"; print_r($rows); die;

	require_once( $GLOBALS['mosConfig_absolute_path'] . '/administrator/includes/pageNavigation.php' );
	$pageNav = new mosPageNav( $total, $limitstart, $limit );

	HTML_output::listEntries( $rows, $search, $pageNav );
}

function cancelEdit() {
	global $option;
	mosRedirect( "index2.php?option=$option" );
}

function saveEntry() {
	global $database, $option, $task;

	if ($task=="apply") {
		$msg = 'Successfully Saved Changes';
		mosRedirect( "index2.php?option=$option&task=edit&hidemainmenu=1&id=". $row->id, $msg );
	} else {
		$msg = 'Item Successfully Saved';
		mosRedirect( "index2.php?option=$option&mosmsg=$msg" );
	}
}

function removeEntry() {
	global $database, $option;

	$cid = mosGetParam( $_POST, 'cid', array(0) );
	$total = count( $cid );
	if ($total < 1) {
		echo "<script> alert('Select an item to delete'); window.history.go(-1);</script>\n";
		exit;
	}
	if (count( $cid )) {
		$cids = implode( ',', $cid );
		$database->setQuery( "DELETE FROM #__myipn_sub_paypal_log WHERE id IN ($cids)" );
		if (!$database->query()) {
			echo "<script> alert('".$database->getErrorMsg()."'); window.history.go(-1); </script>\n";
		}
	}
	$msg = $total ." Item(s) Successfully Removed";
	mosRedirect( "index2.php?option=$option&mosmsg=$msg" );
}

function editConfig() {
	global $database, $option;

	$row = new mosMyipnConfig( $database );
	$row->load( 1 );

	$row->sandbox = mosHTML::yesnoRadioList( 'sandbox', 'class="inputbox"', $row->sandbox );

	HTML_output::editConfig( $row );
}

function saveConfig() {
	global $database, $option, $task;

	$row = new mosMyipnConfig( $database );
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}

	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}

	if ($task=="applyconfig") {
		$msg = 'Successfully Saved Changes';
		mosRedirect( "index2.php?option=$option&task=config&id=". $row->id, $msg );
	} else {
		$msg = 'Configuration Successfully Saved';
		mosRedirect( "index2.php?option=$option&mosmsg=$msg" );
	}
}

?>
